package com.objetdirect.gwt.umldrawer.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.widgets.MessageBox;
import com.objetdirect.gwt.umldrawer.shared.services.DeveloperService;
import com.objetdirect.gwt.umldrawer.shared.services.DeveloperServiceAsync;

public class SignInPage implements PageContent{

	private VerticalPanel mainPanel = new VerticalPanel() ;
	private VerticalPanel signInFields = new VerticalPanel();
	private HorizontalPanel signInWithImage = new HorizontalPanel();
	private VerticalPanel signInPanel = new VerticalPanel() ;
	
	private Label signInLabel = new Label();
	private Label e_mailLabel = new Label() ;
	private Label passwordLabel = new Label() ;
	private TextBox e_mail = new TextBox();
	private PasswordTextBox password = new PasswordTextBox();
	private Image signInButton = new Image("images/signIn.png");
	private Hyperlink forgotPassword = new Hyperlink("Forgot Your Password?" , "");
	private Hyperlink getAccount = new Hyperlink("Don't Have An Account?", "SignUp");
	private Image header = new Image();
	private Image footer = new Image();
	private Image umlImage = new Image();
	
	private DeveloperServiceAsync developerService = GWT.create(DeveloperService.class);
	
	@Override
	public void setPageContent() {
		// TODO Auto-generated method stub
		
		header.setUrl("images/header.png");
		header.setWidth("1201px");
		header.setStyleName("headerImage");
		
		signInLabel.setText("Sign In");
		signInLabel.setStyleName("pagesHeader");
		
		e_mailLabel.setText("Email :");
		e_mailLabel.setStyleName("labels");
		
		passwordLabel.setText("Password :");
		passwordLabel.setStyleName("labels");
		
		e_mail.setStyleName("textBox");
		password.setStyleName("textBox");
		
		forgotPassword.setStyleName("labels");
		getAccount.setStyleName("labels");
		
		signInButton.setStyleName("ProDesUMLButton");
		
		footer.setUrl("images/footer.png");
		footer.setWidth("1201px");
		
		signInFields.add(new HeaderButtons().getHeaderButtons());
		signInFields.add(signInLabel);
		signInFields.add(e_mailLabel);
		signInFields.add(e_mail);
		signInFields.add(passwordLabel);
		signInFields.add(password);
		signInFields.add(forgotPassword);
		signInFields.add(getAccount);
		signInFields.add(signInButton);
		
		signInButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				singIn();
			}
		});
		
		signInButton.addMouseMoveHandler(new MouseMoveHandler() {
			@Override
			public void onMouseMove(MouseMoveEvent event) {
				signInButton.setUrl("images/signIn_.png");
			}
		});
		
		signInButton.addMouseOutHandler(new MouseOutHandler() {
			@Override
			public void onMouseOut(MouseOutEvent event) {
				signInButton.setUrl("images/signIn.png");
			}
		});
		
		umlImage.setUrl("images/image.jpg");
		umlImage.setStyleName("homeLeftImage");
		
		signInWithImage.add(umlImage);
		signInWithImage.add(signInFields);
		
		signInPanel.add(signInWithImage);
		signInPanel.add(new PrivacyBar().getprivacyBar());
		signInPanel.setStyleName("border");
		
		mainPanel.add(header);
		mainPanel.add(signInPanel);
		mainPanel.add(footer);
	}
	
	@Override
	public VerticalPanel getPageContent() {
		// TODO Auto-generated method stub
		setPageContent() ;
		return mainPanel ;
	}
	
	public void singIn() {
		String errorMsg = "";
		if (e_mail.getText().equals("")) {
			errorMsg += "- You Must Insert Your E-Mail.<br>";
		}
		if (password.getText().equals("")) {
			errorMsg += "- You Must Insert Your Password.<br>";
		}
		
		if (errorMsg.equals("")) {
			developerService.signIn(e_mail.getText(), password.getText(), new AsyncCallback<Boolean>() {
				
				@Override
				public void onSuccess(Boolean result) {
					if (result == true){
						History.newItem("WorkSpace");
					}
					else {
						MessageBox.alert("Server Error", "Wromg Email Or Password");
					}
				}
				
				@Override
				public void onFailure(Throwable caught) {
					MessageBox.alert("Server Error", "Connection Error");
				}
			});
		}
		else {
			MessageBox.setMinWidth(300);
			MessageBox.alert("Validation Error", errorMsg);
		}
	}
}
